from langchain_community.chat_models import ChatTongyi
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
from llama_index.embeddings.dashscope import (
    DashScopeEmbedding,
    DashScopeTextEmbeddingModels,
    DashScopeTextEmbeddingType
)

#词嵌入模型
embed_model = DashScopeEmbedding(
    model_name=DashScopeTextEmbeddingModels.TEXT_EMBEDDING_V3,
    text_type=DashScopeTextEmbeddingType.TEXT_TYPE_DOCUMENT,
    api_key="sk-f97e3654139742a4b01a99631628d36d"
)

# 初始化LLM
llm = ChatTongyi(model="qwen-plus", api_key="sk-f97e3654139742a4b01a99631628d36d")

Settings.llm = llm
Settings.embed_model = embed_model

#加载文档
documents = SimpleDirectoryReader(r"D:\Code\sshcode\llamaindex").load_data()
index = VectorStoreIndex.from_documents(documents)
#将Langchain的LLM封装成LlamaIndex的LLM
query_engine = index.as_query_engine()
res = query_engine.query("怎么退款")
print(res)

